//
//  HTDriverLocationVC.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/31.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTDriverLocationVC.h"
#import "HTDriverLocationCell.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <MAMapKit/MAMapKit.h>
#import "HTMyAnnotation.h"
#import "HTMyAnnotationView.h"

@interface HTDriverLocationVC ()<UITableViewDelegate,UITableViewDataSource,MAMapViewDelegate>
@property (weak, nonatomic) IBOutlet MAMapView *mapView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mapHeight;

@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) HTDriverLocationModel *currentModel;
@property (nonatomic,strong) HTMyAnnotation *currentAnnotation;

@end

@implementation HTDriverLocationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configTableView];
    [self netLoadData];
}
- (void)configTableView{
    self.title = @"司机定位信息";
    self.mapHeight.constant = kHTScreenHeight - kWidth(375); self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"HTDriverLocationCell" bundle:nil] forCellReuseIdentifier:DriverLocationCell];
    self.mapView.delegate = self;
    self.mapView.showsCompass = NO;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

#pragma mark --- tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HTDriverLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:DriverLocationCell forIndexPath:indexPath];
    cell.locationModel = self.dataSource[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self clear];
    self.currentModel = self.dataSource[indexPath.row];
    [self addAnotationView];
}

#pragma mark --- 网络请求
- (void)netLoadData{
    [self startHud];
    [HTHttpRequestManager requestGetAddressListWithCarid:self.carid Success:^(id  _Nullable responseObject) {
        [self endHud];
        DBLog(@"获取地点列表：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"获取地点列表失败"];
            return;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        self.dataSource = [HTDriverLocationModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
        [self.tableView reloadData];
        if (self.dataSource.count > 0) {
            self.currentModel = self.dataSource[0];
        }
        [self clear];
        [self addAnotationView];
    } failure:^(NSError * _Nonnull error) {
        [self endHud];
    }];
    
}
#pragma mark --- 地图
//添加气泡
- (void)addAnotationView{
    if (self.currentModel == nil) {
        return;
    }
   self.currentAnnotation = [[HTMyAnnotation alloc]init];
   self.currentAnnotation.coordinate = CLLocationCoordinate2DMake((CLLocationDegrees)[self.currentModel.lat doubleValue], (CLLocationDegrees)[ self.currentModel.lng doubleValue]);
    self.currentAnnotation.state = @"1";
    [self.mapView addAnnotation:self.currentAnnotation];
    [self.mapView setCenterCoordinate:self.currentAnnotation.coordinate animated:YES];
}
- (void)mapView:(MAMapView *)mapView mapDidZoomByUser:(BOOL)wasUserAction{
    
}
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view{
    view.selected = NO;
}
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    if ([annotation isKindOfClass:[HTMyAnnotation class]]) {
        HTMyAnnotationView *view = (HTMyAnnotationView*) [(HTMyAnnotation *)annotation getAnnotationView:mapView];
        view.ID = self.carid;
        return view;
    }
    return nil;
}
- (void)clear{
    if (self.currentAnnotation) {
        [self.mapView removeAnnotation:self.currentAnnotation];
    }
}
@end
